package org.wu.framework.log.platform.infrastructure.persistence;

import org.wu.framework.log.platform.infrastructure.entity.RequestLogDO;
import org.wu.framework.log.platform.infrastructure.converter.RequestLogConverter;
import org.wu.framework.log.platform.infrastructure.mapper.RequestLogMapper;
import org.wu.framework.log.platform.domain.model.request.log.RequestLogRepository;
import org.springframework.stereotype.Repository;

import java.util.stream.Collectors;

import org.wu.framework.lazy.orm.database.lambda.stream.wrapper.LazyWrappers;
import org.wu.framework.web.response.Result;
import org.wu.framework.web.response.ResultFactory;
import jakarta.annotation.Resource;
import org.wu.framework.log.platform.domain.model.request.log.RequestLog;
import org.wu.framework.lazy.orm.database.lambda.stream.lambda.LazyLambdaStream;

import java.util.List;

import org.wu.framework.lazy.orm.database.lambda.domain.LazyPage;

/**
 * describe 请求日志
 *
 * @author Jia wei Wu
 * @date 2024/03/25 04:20 下午
 * @see org.wu.framework.lazy.orm.core.persistence.reverse.lazy.ddd.DefaultDDDLazyInfrastructurePersistence
 **/
@Repository
public class RequestLogRepositoryImpl implements RequestLogRepository {

    @Resource
    LazyLambdaStream lazyLambdaStream;

    /**
     * describe 新增请求日志
     *
     * @param requestLog 新增请求日志
     * @return {@link Result<RequestLog>} 请求日志新增后领域对象
     * @author Jia wei Wu
     * @date 2024/03/25 04:20 下午
     **/

    @Override
    public Result<RequestLog> story(RequestLog requestLog) {
        RequestLogDO requestLogDO = RequestLogConverter.INSTANCE.fromRequestLog(requestLog);
        lazyLambdaStream.upsert(requestLogDO);
        return ResultFactory.successOf();
    }

    /**
     * describe 批量新增请求日志
     *
     * @param requestLogList 批量新增请求日志
     * @return {@link Result<List<RequestLog>>} 请求日志新增后领域对象集合
     * @author Jia wei Wu
     * @date 2024/03/25 04:20 下午
     **/

    @Override
    public Result<List<RequestLog>> batchStory(List<RequestLog> requestLogList) {
        List<RequestLogDO> requestLogDOList = requestLogList.stream().map(RequestLogConverter.INSTANCE::fromRequestLog).collect(Collectors.toList());
        lazyLambdaStream.upsert(requestLogDOList);
        return ResultFactory.successOf();
    }

    /**
     * describe 查询单个请求日志
     *
     * @param requestLog 查询单个请求日志
     * @return {@link Result<RequestLog>} 请求日志领域对象
     * @author Jia wei Wu
     * @date 2024/03/25 04:20 下午
     **/

    @Override
    public Result<RequestLog> findOne(RequestLog requestLog) {
        RequestLogDO requestLogDO = RequestLogConverter.INSTANCE.fromRequestLog(requestLog);
        RequestLog requestLogOne = lazyLambdaStream.selectOne(LazyWrappers.lambdaWrapperBean(requestLogDO), RequestLog.class);
        return ResultFactory.successOf(requestLogOne);
    }

    /**
     * describe 查询多个请求日志
     *
     * @param requestLog 查询多个请求日志
     * @return {@link Result<List<RequestLog>>} 请求日志领域对象
     * @author Jia wei Wu
     * @date 2024/03/25 04:20 下午
     **/

    @Override
    public Result<List<RequestLog>> findList(RequestLog requestLog) {
        RequestLogDO requestLogDO = RequestLogConverter.INSTANCE.fromRequestLog(requestLog);
        List<RequestLog> requestLogList = lazyLambdaStream.selectList(LazyWrappers.lambdaWrapperBean(requestLogDO), RequestLog.class);
        return ResultFactory.successOf(requestLogList);
    }

    /**
     * describe 分页查询多个请求日志
     *
     * @param size       当前页数
     * @param current    当前页
     * @param requestLog 分页查询多个请求日志
     * @return {@link Result<LazyPage<RequestLog>>} 分页请求日志领域对象
     * @author Jia wei Wu
     * @date 2024/03/25 04:20 下午
     **/

    @Override
    public Result<LazyPage<RequestLog>> findPage(int size, int current, RequestLog requestLog) {
        RequestLogDO requestLogDO = RequestLogConverter.INSTANCE.fromRequestLog(requestLog);
        LazyPage<RequestLog> lazyPage = new LazyPage<>(current, size);
        LazyPage<RequestLog> requestLogLazyPage = lazyLambdaStream
                .selectPage(
                        LazyWrappers.lambdaWrapperBean(requestLogDO)
                                .orderByDesc(RequestLogDO::getCreateTime)
                        , lazyPage, RequestLog.class);
        return ResultFactory.successOf(requestLogLazyPage);
    }

    /**
     * describe 删除请求日志
     *
     * @param requestLog 删除请求日志
     * @return {@link Result<RequestLog>} 请求日志
     * @author Jia wei Wu
     * @date 2024/03/25 04:20 下午
     **/

    @Override
    public Result<RequestLog> remove(RequestLog requestLog) {
        RequestLogDO requestLogDO = RequestLogConverter.INSTANCE.fromRequestLog(requestLog);
        //  lazyLambdaStream.delete(LazyWrappers.lambdaWrapperBean(requestLogDO));
        return ResultFactory.successOf();
    }

    /**
     * describe 是否存在请求日志
     *
     * @param requestLog 请求日志领域对象
     * @return {@link Result<Boolean>} 是否存在 true 存在，false 不存在
     * @author Jia wei Wu
     * @date 2024/03/25 04:20 下午
     **/

    @Override
    public Result<Boolean> exists(RequestLog requestLog) {
        RequestLogDO requestLogDO = RequestLogConverter.INSTANCE.fromRequestLog(requestLog);
        Boolean exists = lazyLambdaStream.exists(LazyWrappers.lambdaWrapperBean(requestLogDO));
        return ResultFactory.successOf(exists);
    }

}